Skip to content

feat(auth,storage): add session cache fast-path and storage identifier configuration - #4191

Open
sean-escendant wants to merge 5 commits into
aws-amplify:mainfrom
escendant-ai:feat/session-cache-and-storage-identifiers
Open

feat(auth,storage): add session cache fast-path and storage identifier configuration#4191
sean-escendant wants to merge 5 commits into
aws-amplify:mainfrom
escendant-ai:feat/session-cache-and-storage-identifiers

Conversation

@sean-escendant

@sean-escendant sean-escendant commented Apr 10, 2026

Copy link
Copy Markdown

Issue

Related: #1102, #3340

Description

This PR introduces two improvements:

  1. Auth session cache fast-path: Adds an in-memory cache for fetchAuthSession() that bypasses the TaskQueue when tokens are still valid (with a 2-minute expiry buffer). This eliminates ~300-1000ms of serialization overhead during concurrent token fetches at app startup. The cache is thread-safe (NSLock) and cleared on signOut().

  2. Storage identifier configuration: Adds sessionIdentifier and sharedContainerIdentifier options to the Storage plugin configuration (AmplifyOutputsData.Storage and JSON config). This enables customization of URLSession identifiers and app group shared container identifiers for background transfer and extension support.

General Checklist

  • Added new tests to cover change, if needed
  • Build succeeds with all target using Swift Package Manager
  • All unit tests pass
  • All integration tests pass
    • Integration tests require AWS backend provisioning and are not applicable for this fork PR
  • Security oriented best practices and standards are followed (e.g. using input sanitization, principle of least privilege, etc)
    • Thread-safe cache with NSLock, cleared on sign-out, in-memory only
  • Documentation update for the change if required
    • Added inline documentation for StorageConfiguration, AmplifyOutputsData.Storage properties, session cache, and areTokensValid()
  • PR title conforms to conventional commit style
  • New or updated tests include Given When Then inline code documentation and are named accordingly testThing_condition_expectation()
  • If breaking change, documentation/changelog update with migration instructions
    • No breaking changes — all new parameters are optional with defaults

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

sean-escendant and others added 4 commits April 10, 2026 17:15
Cache the AWSAuthCognitoSession directly and return it immediately when:
- forceRefresh is false
- Tokens are valid (not expired within 2-minute buffer)

This avoids TaskQueue serialization overhead (~300-1000ms) for concurrent
token fetches during app startup when a valid session already exists.

Changes:
- Add areTokensValid() to AWSAuthCognitoSession
- Add thread-safe cachedSession property to AWSCognitoAuthPlugin
- Add fast-path cache check in fetchAuthSession before TaskQueue
- Clear cache immediately on signOut
…tifiers

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…orage identifiers

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@sean-escendant
sean-escendant marked this pull request as ready for review April 10, 2026 16:37
@sean-escendant
sean-escendant requested a review from a team as a code owner April 10, 2026 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant